import course_api from '../../../repo/api/course_api'

export const CourseType = {
  VIDEO: 1, AUDIO: 2, DOC: 3, EXAM: 4
};
///获取课程详情底部tab的各个类型列表数据
///@param list 用来保存数据
const fetchCourseData = (courseId, type, list) => {
  return course_api.getSubjectContain(courseId, type)
    .then((res) => {  //subjectId:主题id,type=1:视频 2:音频  3:文档 4:测试
      let responseData = res.responseData || {};
      let courses = responseData.list;

      if (type === CourseType.VIDEO || type === CourseType.AUDIO) {
        courses.forEach(course => {
          course.mediaList.forEach(media => {
            media.courseType = type
            media.courseTitle = course.title
          })
        })
      }

      list.length = 0;
      list.push(...courses);
      return list
    })
};
//获取ocurse mediaList 里面的数据，判断是否有图片
const getMediaListByCourseList = (courseList, coursePic) => {
  let result = [];
  for (let i = 0; i < courseList.length; i++) {
    let course = courseList[i];
    if (course.mediaList) {
      for (let j = 0; j < course.mediaList.length; j++) {
        let media = course.mediaList[j];
        let picUrl = media.picUrl;
        if (!(picUrl && picUrl.startsWith('http'))) {
          media.picUrl = coursePic
        }
        result.push(media)
      }
    }
  }
  return result
};
//PC端medialist组合
const getMediaListPC = (courseList, coursePic) => {
  let result = [];
  for (let i = 0; i < courseList.length; i++) {
    let course = courseList[i];
    if (course.mediaList) {
      for (let j = 0; j < course.mediaList.length; j++) {
        let media = course.mediaList[j];
        let picUrl = media.picUrl;
        if (!(picUrl && picUrl.startsWith('http'))) {
          media.picUrl = coursePic
        }
      }
      result.push(course)
    }
  }
  return result
};

export default {
  fetchCourseData,
  getMediaListByCourseList,
  getMediaListPC,
}
